using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BerkoRules.Objects;
using BerkoRules.Mappers;
using Microsoft.VisualBasic.CompilerServices;

namespace Berko
{
    public partial class frmAlarmas : Form
    {
        private int _alaID;
        private string _alarma;
        private int _cliID;

        public int Cli_ID
        {
            get { return _cliID; }
            set { _cliID = value; }
        }


        public string Alarma
        {
            get { return _alarma; }
            set { _alarma = value; }
        }


        public int Ala_ID
        {
            get
            {
                return this._alaID;
            }
            set
            {
                this._alaID = value;
            }
        }
 
        public frmAlarmas()
        {
            InitializeComponent();
        }

        private void Cargar_clientes()
        {
            try
            {
                this.cboClientes.DataSource = ClientesMapper.Instance().GetAll();
                this.cboClientes.ValueMember = "cli_ID";
                this.cboClientes.DisplayMember = "cli_Nombre";
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
            }
        }

        private void cmdGuardar_Click(object sender, EventArgs e)
        {
            if (this.Validar_Datos() && this.Guardar())
            {
                MessageBox.Show("La alarma se guard\x00f3 correctamente", "Correcto", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.Close();
            }
        }
        private bool Validar_Datos()
        {
            if (this.cboClientes.SelectedValue == null)
            {
                MessageBox.Show("Debe seleccionar un cliente v\x00e1lido", "Datos incorrectos", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.cboClientes.Focus();
                return false;
            }
            if (this.txtAlarma.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Debe cargar una alarma", "Datos incorrectos", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                this.txtAlarma.Focus();
                return false;
            }
            return true;
        }

        private void Traer_Alarma()
        {
            AlarmasObject one = null;
            try
            {
                one = AlarmasMapper.Instance().GetOne(this._alaID);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                ProjectData.ClearProjectError();
            }
            if (one != null)
            {
                this.cboClientes.SelectedValue = one.cli_ID;
                this.txtAlarma.Text = one.ala_Alarma;
                this.txtFecha.Text = one.ala_Fecha.ToLongDateString();
                this.txtHora.Text = one.ala_Hora;
                this.chkRealizada.Checked = one.ala_Realizada;
            }
        }

 


        private void cmdSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmAlarmas_Load(object sender, EventArgs e)
        {
            this.Cargar_clientes();
            if (this._alaID != 0)
            {
                this.Traer_Alarma();
            }
            else if (this._cliID > 0)
            {
                this.cboClientes.SelectedValue = this._cliID;
                this.txtAlarma.Text = this._alarma;
                this.txtAlarma.Focus();
            }
        }

        private bool Guardar()
        {
            AlarmasObject row = new AlarmasObject(this._alaID);
            /*if (this._alaID != 0)
            {
                row.Ala_ID2 = this._alaID;
            }*/
            row.cli_ID = (int)this.cboClientes.SelectedValue;
            row.ala_Alarma = this.txtAlarma.Text;
            row.ala_Fecha = this.txtFecha.Value.Date;
            row.ala_Hora = this.txtHora.Text;
            row.ala_Realizada = this.chkRealizada.Checked;
            try
            {
                if (this._alaID != 0)
                {
                    AlarmasMapper.Instance().Save(row);
                }
                else
                {
                    AlarmasMapper.Instance().Insert(row);
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                bool flag = false;
                return flag;
            }
            return true;
        }

    }
}